package cn.hncu.booksManagementSystem.in.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.BMS_APP;
import cn.hncu.booksManagementSystem.book.business.ebi.BookEbi;
import cn.hncu.booksManagementSystem.book.business.factory.BookEbiFactory;
import cn.hncu.booksManagementSystem.in.business.ebi.InDetailEbi;
import cn.hncu.booksManagementSystem.in.business.factory.InDetailEbiFactory;
import cn.hncu.booksManagementSystem.in.business.factory.InEbiFactory;
import cn.hncu.booksManagementSystem.in.vo.InDetailModel;
import cn.hncu.booksManagementSystem.in.vo.InMainModel;
import javax.swing.JScrollPane;

public class InPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private JFrame mainFrame;
	private JList<String> JList;
	private List<InDetailModel> details =null;
	//注入ebi
	InDetailEbi inDetaEbi = InDetailEbiFactory.getInstanceOfInDetailEbi();
	BookEbi bookEbi = BookEbiFactory.getInstanceOfBookEbi();
	private JButton btnSubmit;
			
	/**
	 * Create the panel.
	 * @param mainFrame 
	 * @wbp.parser.constructor
	 */
	public InPanel(JFrame mainFrame) {
		this.mainFrame = mainFrame;
		ListPanel.initStaticAttribute();
		details = new ArrayList<InDetailModel>();
		initContentPane();
		initListData();
	}
	
	

	public InPanel(JFrame mainFrame, List<InDetailModel> details) {
		this.mainFrame = mainFrame;
		this.details = details;
		initContentPane();
		initListData();
		btnSubmit.setEnabled(true);
	}

	private void initListData() {
//		//注意：new InDetailModel[0]是属于类模板    内存中并没有InDetailModel类的一个实例，
//		//所以InDetailModel中的ebi需要static，否则空指针异常(因为会调用到toString方法)
//		InDetailQueryModel idqm = new InDetailQueryModel();
//		idqm.setInId(inMainId);
//		JList.setListData(inDetaEbi.getInDetailsByCondition(idqm).toArray(new InDetailModel[0]));
		String dispayDetails[] = new String[details.size()];
		for (int i = 0; i < details.size(); i++) {
			dispayDetails[i] = "第"+(i+1)+"记录：《"+bookEbi.getBookModelById(details.get(i).getBookId()).getName()+"》, "+details.get(i).getSumNum()+"本";
		}
		JList.setListData(dispayDetails);
	}



	private void initContentPane() {
		setLayout(null);
		
		JLabel label = new JLabel("订单");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(311, 35, 196, 47);
		add(label);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(117, 113, 574, 222);
		add(scrollPane);
		
		JList = new JList<String>();
		scrollPane.setViewportView(JList);
		JList.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		
		JButton btnToAddPanel = new JButton("添加...");
		btnToAddPanel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnToAddPanelActionPerformed(e);
			}
		});
		btnToAddPanel.setForeground(Color.BLUE);
		btnToAddPanel.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnToAddPanel.setBounds(218, 363, 130, 47);
		add(btnToAddPanel);
		
		JButton btnToUpdatePanel = new JButton("修改...");
		btnToUpdatePanel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnToUpdatePanelActionPerformed(e);
			}
		});
		btnToUpdatePanel.setForeground(Color.BLUE);
		btnToUpdatePanel.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnToUpdatePanel.setBounds(465, 363, 130, 47);
		add(btnToUpdatePanel);
		
		btnSubmit = new JButton("提交订单");
		btnSubmit.setEnabled(false);
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnSubmitActionPerformed(e);
			}
		});
		btnSubmit.setForeground(Color.BLUE);
		btnSubmit.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnSubmit.setBounds(218, 430, 130, 47);
		add(btnSubmit);
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.setContentPane(new ListPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnBack.setForeground(Color.BLUE);
		btnBack.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnBack.setBounds(465, 430, 130, 47);
		add(btnBack);
	}
	
	protected void btnToUpdatePanelActionPerformed(ActionEvent e) {
		int index = JList.getSelectedIndex();
		if (index<0) {
			JOptionPane.showMessageDialog(mainFrame, "请选择你要修改的一条记录");
			return;
		}
		mainFrame.setContentPane(new UpdatePanel(mainFrame,details,index));
		mainFrame.validate();
		
	}



	protected void btnSubmitActionPerformed(ActionEvent e) {
		//1收集参数inMain(id 缺, inUserId 当前用户 , inUserName---为外键补的有个属性,提高用户体验  , inDate 缺)和
		//details(id-缺 , inId-缺 , bookId-有 , sumNum-有 , sumMoney-缺)
		//2组织参数
		InMainModel inMain = new InMainModel();
		inMain.setInUserId(BMS_APP.activeUser.getId()); //inUserId
		inMain.setInUserName(BMS_APP.activeUser.getName()); //inUserName
		//3调用逻辑层
		boolean boo = InEbiFactory.getInEbi().add(inMain, details);
		//4导向结果界面
		if (boo) {
			mainFrame.setContentPane(new ListPanel(mainFrame));
			mainFrame.validate();
		}else {
			JOptionPane.showMessageDialog(mainFrame, "提交订单失败");
			initListData();
		}
	}


	protected void btnToAddPanelActionPerformed(ActionEvent e) {
		mainFrame.setContentPane(new AddPanel(mainFrame,details));
		mainFrame.validate();
	}
}
